package com.myway.service.ibe.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.myway.dto.domain.Cabin;
import com.myway.dto.domain.FlightCriteria;
import com.myway.dto.ibe_param.FlightParam;
import com.myway.dto.param.CabinsQuantityParam;
import com.myway.service.ibe.command.AVCommand;
import com.myway.service.ibe.command.SearchFlightCommand;
import com.myway.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yufei on 2018/4/2.
 */
@Controller
@RequestMapping("/av")
public class AVController {

    @Autowired
    private SearchFlightCommand searchFlightCommand;

    @Autowired
    private AVCommand avCommand;

    @RequestMapping("/flights")
    @ResponseBody
    public String searchFlights(@RequestBody String json){
        //TODO:调用IBE接口查询航班信息
        FlightCriteria params = JSON.parseObject(json,new TypeReference<FlightCriteria>(){});
        String jsonResult = searchFlightCommand.execute(params);
        return jsonResult;
    }
    @RequestMapping("/cabin")
    @ResponseBody
    public List<Cabin> getCabin(@RequestBody String json){
        //TODO:调用IBE接口查询航班信息
        CabinsQuantityParam params = JSON.parseObject(json,new TypeReference<CabinsQuantityParam>(){});
        List<Cabin> cabins = avCommand.getCabinNum(params);
        return cabins;
    }
    @RequestMapping("/cabintest")
    @ResponseBody
    public String getCabin( ){
        //TODO:调用IBE接口查询航班信息
        CabinsQuantityParam params = new CabinsQuantityParam();
        params.setFlightDate("2018-08-22");
        params.setOrg("TBS");
        params.setDst("HRK");
        params.setAirline("MJ");
        List<Cabin> cabinlist=new ArrayList<Cabin>();
        Cabin cabin=new Cabin();
        cabin.setCabinName("T");
        cabinlist.add(cabin);
        params.setCabins(cabinlist);
        List<Cabin> cabins = avCommand.getCabinNum(params);
        return JSON.toJSONString(cabins);
    }

    @RequestMapping("/cabinNum")
    @ResponseBody
    public String getCabinNum(@RequestBody String json){
        CabinsQuantityParam params = JSON.parseObject(json,new TypeReference<CabinsQuantityParam>(){});
        //TODO:调用IBE接口查询航班信息
        List<Cabin> cabins = avCommand.getCabinNum(params);
        return JSON.toJSONString(cabins);
    }

    @RequestMapping("/flyDistance")
    @ResponseBody
    public String getFlyDistance(@RequestBody String json){
        FlightParam param = JSON.parseObject(json,FlightParam.class);
        return avCommand.getFlyDistance(param.getFlightNo(), DateUtils.StringToDate(param.getFlightDate(),"yyyy-MM-dd"));
    }

    @RequestMapping("/getMealList")
    @ResponseBody
    public String getMealList(@RequestBody String json){
        FlightParam param = JSON.parseObject(json,FlightParam.class);
        return JSON.toJSONString(avCommand.getMealList(param.getFlightNo(), DateUtils.StringToDate(param.getFlightDate(),"yyyy-MM-dd")));
    }

}
